<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from "echarts";
require("echarts/theme/macarons"); // echarts theme
import resize from "./mixins/resize";

const animationDuration = 6000;

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "100%"
    },
    height: {
      type: String,
      default: "400px"
    },
    totalSales: {
      type: String,
      default: "0"
    },
    dataList: {
      type: [Object, Array],
      default: () => {}
    },
    dataValue: {
      type: [Object, Array],
      default: () => {}
    }
  },
  data() {
    return {
      chart: null
    };
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart();
    });
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, "macarons");
      this.chart.setOption({
        tooltip: {
          trigger: "item",
          formatter: "{a} <br/> {c}元 ({d}%)"
        },
        legend: {
          orient: "vertical",
          x: "right",
          data: this.dataList
        },
        textStyle: {
          fontSize: 18
        },
        graphic: {
          type: "text",
          left: "center",
          top: "center",
          style: {
            text: this.totalSales,
            textAlign: "center",
            fill: this.$store.state.common.theme,
            width: 50,
            height: 50,
            fontSize: 18
          }
        },
        color: [
          "rgb(203,155,255)",
          "rgb(149,162,255)",
          "rgb(58,186,255)",
          "rgb(119,168,249)",
          "rgb(235,161,159)"
        ], //五个数据，五个颜色
        series: [
          {
            name: "",
            type: "pie",
            radius: ["40%", "60%"],
            center: ["50%", "50%"],
            data: this.dataValue
          }
        ]
      });
    }
  }
};
</script>
